<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/25
 * Time: 11:56
 */
namespace app\web\controller;

use app\web\service\TemplateService;

class Template extends Base {
    /**
     * @todo 获取打印模版
     * @author ZJF
     */
    public function getTemplate(){
        $user=request()->user;//登录信息
        $type = request()->param("type",1);//1快递模版，2发货单模版
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($type) || !is_numeric($type) || !in_array($type,[1,2])){
            return json_ok([],1,'无效的模版类型');
        }
        $temp_service = new TemplateService();
        $res = $temp_service->getTemplate($shop_id,$type);
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 获取平台全部模版
     * @author ZJF
     */
    public function getExpressTemp(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $temp_service = new TemplateService();
        $res = $temp_service->getExpressList();
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 店铺添加快递模版
     * @author ZJF
     */
    public function addShopTemp(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $express_id = request()->param("express_id",0);//快递ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($express_id) || !is_numeric($express_id)){
            return json_ok([],1,'无效的快递ID');
        }
        $temp_service = new TemplateService();
        $res = $temp_service->addExpressTemp($shop_id,$express_id);
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 设置默认模版
     * @author ZJF
     */
    public function setInvoiceDef(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $temp_id = request()->param("temp_id",0);//快递ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($temp_id) || !is_numeric($temp_id)){
            return json_ok([],1,'无效的模版ID');
        }
        $temp_service = new TemplateService();
        $res = $temp_service->setInvoiceDef($shop_id,$temp_id);
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 获取店铺设置的默认发货单
     * @author ZJF
     */
    public function getInvoiceDef(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $temp_service = new TemplateService();
        $res = $temp_service->getInvoiceDef($shop_id);
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }
}